package com.arcsoft.music_player.view;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Dialog;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.arcsoft.music_player.R;
import com.arcsoft.music_player.module.data_provider.MusicDetailDataItem;
import com.arcsoft.music_player.utils.RuntimeConfig;

public class SnsDlg {
	protected Dialog mSnsDlg = null;
	protected LinearLayout mSnsLayout = null;
	protected MusicDetailDataItem mPlayingDataItem = null;
	protected LayoutInflater mInflater = null;
	protected Activity mContext = null;
	protected ListView mListView = null;
	protected SnsDlgAdapter mAdapter = null;
	protected String[] mShareItems = null;
	protected String mShareTitle = null;
	protected String mSingerSearchTitle = null;
	protected String[] mSingerSearchItems = null;

	public SnsDlg(Activity context, MusicDetailDataItem item) {
		if (null == context)
			return;
		mContext = context;
		mPlayingDataItem = item;
		iniData();
		initViews();
	}

	protected void initViews() {
		if (null == mContext)
			return;
		mSnsDlg = new Dialog(mContext, R.style.full_high_dlg);
		mSnsDlg.setContentView(R.layout.sns_dlg);

		mListView = (ListView) mSnsDlg.findViewById(R.id.snsList);
		mAdapter = new SnsDlgAdapter(mContext);
		mListView.setAdapter(mAdapter);
		mListView.setOnItemClickListener(mOnItemClickListener);
	}

	public void iniData() {
		if (null == mContext)
			return;
		mShareTitle = mContext.getResources().getString(R.string.str_sns_share);
		mShareItems = mContext.getResources().getStringArray(R.array.share);

		mSingerSearchTitle = mContext.getResources().getString(
				R.string.str_sns_singer_search);
		mSingerSearchItems = mContext.getResources().getStringArray(
				R.array.search_singer);
	}

	protected OnItemClickListener mOnItemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			if (null == mAdapter)
				return;
			String title = (String) arg1.getTag();
			if (null == title)
				return;
			try {
				// share: Twitter
				if (title.equals(makeTag(mShareTitle, mShareItems[0]))) {
					shareToTwitter(
							mContext,
							mContext.getResources().getString(
									R.string.str_share_song)
									+ mPlayingDataItem.musicTitle);
				}
				// share:Facebook
				else if (title.equals(makeTag(mShareTitle, mShareItems[1]))) {
					shareToFacebook(mContext, mContext.getResources()
							.getString(R.string.str_share_song)
							+ mPlayingDataItem.musicTitle);
				}
				// share:Mixi
				else if (title.equals(makeTag(mShareTitle, mShareItems[2]))) {
					toastShareOrSearchFailed();
					return;
				}
				// singer_search:web
				else if (title.equals(makeTag(mSingerSearchTitle,
						mSingerSearchItems[0]))) {
					searchOnWeb(mContext, mPlayingDataItem.musicArtist);
				}
				// singer_search:Wikipedia
				else if (title.equals(makeTag(mSingerSearchTitle,
						mSingerSearchItems[1]))) {
					openBrowser(mContext, "http://ja.wikipedia.org/wiki/"
							+ mPlayingDataItem.musicArtist);
				}
				// singer_search:Facebook
				else if (title.equals(makeTag(mSingerSearchTitle,
						mSingerSearchItems[2]))) {
					openBrowser(mContext,
							"http://www.facebook.com/search.php?q="
									+ mPlayingDataItem.musicArtist);
				}
				// singer_search:Mixi
				else if (title.equals(makeTag(mSingerSearchTitle,
						mSingerSearchItems[3]))) {
					toastShareOrSearchFailed();
					return;
				}
				// Singer_search:You tube
				else if (title.equals(makeTag(mSingerSearchTitle,
						mSingerSearchItems[4]))) {
					searchOnYoutube(mContext, mPlayingDataItem.musicArtist);
				}

			} catch (Exception e) {
				e.printStackTrace();
				toastShareOrSearchFailed();
			}
		}
	};

	public class SnsDlgItem {
		public String title = null;
		public int groupIndex = -1;

		public SnsDlgItem(String title, int titleIndex) {
			this.title = title;
			this.groupIndex = titleIndex;
		}
	}

	private String makeTag(String title, String content) {
		return title + content;
	}

	private void toastShareOrSearchFailed() {
		Toast.makeText(mContext, R.string.str_toast_cannot_share_or_search,
				Toast.LENGTH_SHORT).show();
	}

	public static void shareToTwitter(Context context, String title) {
		if (null == context || null == title)
			return;
		Intent intent = new Intent(Intent.ACTION_VIEW);
		intent.setType("text/plain");
		intent.putExtra(Intent.EXTRA_TEXT, title);
		intent.setClassName("com.twitter.android",
				"com.twitter.android.PostActivity");
		context.startActivity(intent);
	}

	public static void shareToFacebook(Context context, String title) {
		if (null == context || null == title)
			return;
		Intent intent = new Intent(Intent.ACTION_VIEW);
		intent.setType("text/plain");
		intent.putExtra(Intent.EXTRA_TEXT, title);
		intent.setClassName("com.facebook.katana",
				"com.facebook.katana.ComposerActivity");
		context.startActivity(intent);
	}

	public static void searchOnYoutube(Context context, String artist) {
		if (null == context || null == artist)
			return;
		Intent intent = new Intent(Intent.ACTION_VIEW);
		intent.putExtra(SearchManager.QUERY, artist);
		intent.setClassName("com.google.android.youtube",
				"com.google.android.youtube.app." + RuntimeConfig.FROYO
						+ ".phone.ResultsActivity");
		context.startActivity(intent);
	}

	public static void searchOnWeb(Context context, String artist) {
		if (null == context)
			return;
		Intent intent = new Intent();
		intent.setAction(Intent.ACTION_WEB_SEARCH);
		intent.putExtra(SearchManager.QUERY, artist);
		context.startActivity(intent);
	}

	public static void openBrowser(Context context, String url) {
		if (null == context || null == url)
			return;

		try {
			Intent intent = new Intent("android.intent.action.VIEW",
					Uri.parse(url));
			context.startActivity(intent);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void show() {
		if (null == mSnsDlg)
			return;
		// bindView();
		mSnsDlg.show();
	}

	public void destroy() {
		mSnsLayout = null;
		mContext = null;
		mShareItems = null;
		mSingerSearchItems = null;
		mShareTitle = null;
		mSingerSearchTitle = null;
		mPlayingDataItem = null;
		mSnsDlg = null;
		mInflater = null;

		if (null != mAdapter) {
			mAdapter.destroy();
			mAdapter = null;
		}
		if (null != mSnsDlg) {
			mSnsDlg.dismiss();
			mSnsDlg = null;
		}
	}

	public class SnsDlgAdapter extends BaseAdapter {
		private Activity mContext = null;
		protected ArrayList<SnsDlgItem> mItems = null;
		protected LayoutInflater mInflater = null;

		public SnsDlgAdapter(Activity context) {
			mContext = context;
			if (null == mContext)
				return;

			mInflater = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			this.iniData();
		}

		public void destroy() {
			mInflater = null;
			mContext = null;
			if (null != mItems) {
				mItems.clear();
				mItems = null;
			}
		}

		public void iniData() {
			if (null == mContext)
				return;

			mItems = new ArrayList<SnsDlg.SnsDlgItem>();
			addItem(mContext.getResources().getString(R.string.str_sns_share),
					mContext.getResources().getStringArray(R.array.share));

			addItem(mContext.getResources().getString(
					R.string.str_sns_singer_search), mContext.getResources()
					.getStringArray(R.array.search_singer));
		}

		protected void addItem(String title, String[] content) {
			if (null == mItems)
				return;
			SnsDlgItem newItem = new SnsDlgItem(title, -1);
			mItems.add(newItem);
			int titleIndex = mItems.indexOf(newItem);
			for (String item : content) {
				mItems.add(new SnsDlgItem(item, titleIndex));
			}
		}

		@Override
		public int getCount() {
			if (null == mItems)
				return 0;
			return mItems.size();
		}

		@Override
		public Object getItem(int position) {
			return mItems.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public boolean isEnabled(int position) {
			SnsDlgItem item = (SnsDlgItem) getItem(position);
			if (null == item)
				return false;

			return item.groupIndex >= 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (null == mInflater)
				return convertView;
			SnsDlgItem item = (SnsDlgItem) getItem(position);
			if (null == item)
				return convertView;

			if (item.groupIndex < 0) {
				LinearLayout titleLayout = (LinearLayout) mInflater.inflate(
						R.layout.sns_dlg_title_item, null);
				TextView titleView = (TextView) titleLayout
						.findViewById(R.id.sns_dlg_title);
				if (null == titleView)
					return convertView;
				titleView.setText(item.title);
				return titleLayout;
			} else {
				LinearLayout contentLayout = (LinearLayout) mInflater.inflate(
						R.layout.sns_dlg_content_item, null);
				TextView itemView = (TextView) contentLayout
						.findViewById(R.id.sns_dlg_content);
				if (null == itemView)
					return convertView;
				itemView.setText(item.title);
				SnsDlgItem titleItem = mItems.get(item.groupIndex);
				if (null == titleItem)
					return convertView;
				contentLayout.setTag(makeTag(titleItem.title, item.title));
				return contentLayout;
			}
		}
	}
}
